/**
 * 
 */
package mx.netbrain.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.converter.ProfileConverter;
import mx.netbrain.dao.GroupDao;
import mx.netbrain.dao.GroupUserDao;
import mx.netbrain.dao.impl.GroupDaoImpl;
import mx.netbrain.dao.impl.GroupUserDaoImpl;
import mx.netbrain.model.GroupUser;
import mx.netbrain.model.GroupUserId;
import mx.netbrain.model.Groups;
import mx.netbrain.model.Profile;
import mx.netbrain.model.Status;
import mx.netbrain.model.User;

import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;

/**
 * @author julian
 * 
 */
public class GroupBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3545041210573362767L;
	private String name;
	private String description;
	private List<Groups> listGroup;
	private DualListModel<User> listUsers;
	List<User> source;
	List<User> target;
	private Groups group;
	private Status status = new Status();
	private Profile profile;
	private List<Profile> listProfile;
	private GroupUser groupuser;

	public GroupBean() {
		listProfile = ProfileConverter.profileBD;
	}

	public void prepareGroup(Long id) {
		GroupDao groupDao = new GroupDaoImpl();
		group = groupDao.findById(id);
	}

	public void prepareUserGroup(Long id) {
		GroupDao groupDao = new GroupDaoImpl();
		group = groupDao.findById(id);
		GroupUserDao groupUserDao = new GroupUserDaoImpl();
		source = new ArrayList<User>();
		target = new ArrayList<User>();
		source = groupUserDao.findUserNoGroup(group);
		target = groupUserDao.findUserGroup(group);
		listUsers = new DualListModel<User>(source, target);
	}

	public void save() {
		group.setName(name);
		group.setDescription(description);
		group.setRegistry(new Date());
		status.setId(1);
		group.setStatus(status);
		group.setProfile(profile);
		GroupDao groupDao = new GroupDaoImpl();
		groupDao.save(group);
		group = new Groups();
		status = new Status();
		profile = new Profile();
		name = new String();
		description = new String();
	}

	public void edit() {
		GroupDao groupDao = new GroupDaoImpl();
		groupDao.update(group);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"Datos actualizados correctamente!", "Actualizado"));
		group = new Groups();
	}

	public void delete() {
		status.setId(2);
		group.setStatus(status);
		GroupDao groupDao = new GroupDaoImpl();
		groupDao.delete(group);
		group = new Groups();
		status = new Status();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Groups> getListGroup() {
		GroupDao groupDao = new GroupDaoImpl();
		listGroup = groupDao.listAll();
		return listGroup;
	}

	public void setListGroup(List<Groups> listGroup) {
		this.listGroup = listGroup;
	}

	public DualListModel<User> getListUsers() {
		if (listUsers == null) {
			listUsers = new DualListModel<>();
		}
		return listUsers;
	}

	public void setListUsers(DualListModel<User> listUsers) {
		this.listUsers = listUsers;
	}

	public Groups getGroup() {
		if (group == null) {
			group = new Groups();
		}
		return group;
	}

	public void setGroup(Groups group) {
		this.group = group;
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public List<Profile> getListProfile() {
		return listProfile;
	}

	public void setListProfile(List<Profile> listProfile) {
		this.listProfile = listProfile;
	}

	public List<Profile> completeStorage(String query) {
		List<Profile> suggestions = new ArrayList<Profile>();
		for (Profile p : listProfile) {
			if (p.getProfile().startsWith(query))
				suggestions.add(p);
		}
		return suggestions;
	}

	public void onTransfer(TransferEvent event) {
		for (Object item : event.getItems()) {
			System.out.println(((User) item).getName());
			GroupUserDao groupUserDao = new GroupUserDaoImpl();
			groupuser = groupUserDao.find(((User) item), group);
			if (groupuser != null) {
				groupUserDao.delete(((User) item), group);
				System.out.println("Si existe");
			} else {
				groupuser = new GroupUser();
				GroupUserId groupUserId = new GroupUserId();
				groupUserId.setGroups(group.getId());
				groupUserId.setUsers(((User) item).getId());
				groupuser.setId(groupUserId);
				groupUserDao.save(groupuser);
			}
		}
	}
}
